<?php

    /**
     * Http
     *
     * Handles HTTP request
     * It just gets parameters from the url query and the post content.
     * And responses can be in many format : text, html, xml...
     *
     * @package      Core
     * @subpackage   Controller_Request
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Core_Controller_Request_Http extends Core_Controller_Request_Abstract
    {

    ////////////////////////////////////////////////////////////////////////////
	//                                Variables                               //
	////////////////////////////////////////////////////////////////////////////

        /**
         * The request type code
         *
         * @var string
         */
        protected $_type = 'http';

        /**
         * The type of the default response
         *
         * @var string
         */
        protected $_defaultResponseType = 'http';

	////////////////////////////////////////////////////////////////////////////
	//                            Protected methods                           //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Initializes the parameters of the request
         *
         * @return      void
         */
        protected function _initParams()
        {
            $url = Core_Url::parse($this->_urlScript, $this->_urlPathInfo, $this->_getCleanPostValues($_GET));
            $this->_params = array_merge($url->getParams(), $this->_getCleanPostValues($_POST));
            $this->_params = array_merge($this->_params, $_FILES);
            
            if (isset($_POST['method'])) {
                list($module, $action) = explode('~', $this->_getCleanPostValues($_POST['method']));
                $this->_params['module'] = $module;
                $this->_params['action'] = urldecode($action);
            }
        }

        /**
         * Returns clean data from $_POST
         *
         * @param       mixed   $post
         * @return      mixed
         */
        protected function _getCleanPostValues($post)
        {
            if (is_array($post)) {

                // La variable est un tableau
                $array_clean    = array();

                // Boucle sur la tableau
                foreach ($post as $key => $value) {
                    $array_clean[$key]  = $this->_getCleanPostValues($value);
                }

                return $array_clean;

            } else {

                // La variable n'est pas un tableau

                // Test si les magic quotes sont activées sur le serveur
                if (get_magic_quotes_gpc()) {
                    $value  = stripslashes($post);
                } else {
                    $value  = $post;
                }

                return $value;
            }
        }
    }
